package com.jack.lib.ui.widget

import android.content.Context
import android.widget.RelativeLayout
import kotlin.jvm.JvmOverloads
import android.content.res.TypedArray
import android.util.AttributeSet
import com.jack.lib.ui.R

/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/11/20 14:25
 */
class JRelativeLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0) : RelativeLayout(context, attrs, defStyleAttr) {
    private var mJAutoPress = false

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.JRelativeLayout)
        parseAttr(typedArray)
        typedArray.recycle()
    }

    private fun parseAttr(typedArray: TypedArray) {
        mJAutoPress = typedArray.getBoolean(R.styleable.JRelativeLayout_JAutoPress, false)
    }

    override fun setPressed(pressed: Boolean) {
        super.setPressed(pressed)
        val alpha = if (pressed && isClickable) 0.8f else 1f
        if (mJAutoPress && alpha != getAlpha()) {
            setAlpha(alpha)
        }
    }
}